package com.jming.jike.controller.backend;

import com.jming.jike.common.PageResult;
import com.jming.jike.common.ResponseResult;
import com.jming.jike.common.ValidatorGroup.Insert;
import com.jming.jike.common.ValidatorGroup.Query;
import com.jming.jike.common.ValidatorGroup.Update;
import com.jming.jike.dto.backend.DishDTO;
import com.jming.jike.service.backend.DishService;
import com.jming.jike.util.CommonUtils;
import com.jming.jike.vo.backend.DishDetailVO;
import com.jming.jike.vo.backend.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2022/04/18
 */
@RestController
@RequestMapping("/dish")
@Api(tags = "菜品管理相关接口", value = "/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private CommonUtils commonUtils;

    @PostMapping("/insert")
    @ApiOperation(value = "添加菜品信息", notes = "添加菜品信息", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Boolean> insert(@RequestBody @Validated(Insert.class) DishDTO dishDTO) {

        return ResponseResult.success(dishService.insert(dishDTO));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改菜品信息", notes = "修改菜品信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> update(@RequestBody @Validated({Update.class}) DishDTO dishDTO) {

        return ResponseResult.success(dishService.update(dishDTO));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除菜品信息", notes = "删除菜品信息", produces = "application/json", httpMethod = "DELETE")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {

        return ResponseResult.success(dishService.delete(id));
    }

    @PutMapping("/updateStatus/{id}/{status}")
    @ApiOperation(value = "启/停售菜品", notes = "启/停售菜品", produces = "application/json", httpMethod = "PUT")
    public ResponseResult<Boolean> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {

        return ResponseResult.success(dishService.updateStatus(id, status));
    }

    @PostMapping("/pageQuery")
    @ApiOperation(value = "分页查询菜品列表", notes = "分页查询菜品列表", produces = "application/json", httpMethod = "POST")
    public ResponseResult<PageResult<DishVO>> pageQuery(@RequestBody @Validated({Query.class}) DishDTO dishDTO) {

        return ResponseResult.success(dishService.pageQuery(dishDTO));
    }

    @GetMapping("/queryDetail/{id}")
    @ApiOperation(value = "查询菜品详情", notes = "查询菜品详情", produces = "application/json", httpMethod = "GET")
    public ResponseResult<DishDetailVO> queryDetail(@PathVariable("id") Long id) {

        return ResponseResult.success(dishService.queryDetail(id));
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传菜品图片", notes = "上传菜品图片", produces = "application/json", httpMethod = "POST")
    public ResponseResult<Map<String, String>> uploadImage(MultipartFile file) {

        return ResponseResult.success(commonUtils.uploadImage(file, "dish"));
    }

    @GetMapping("/download")
    @ApiOperation(value = "下载文件", notes = "下载文件", produces = "application/json", httpMethod = "GET")
    public void download(HttpServletResponse res, String fileName) {

        commonUtils.download(fileName, res);
    }
}
